package cn.stylefeng.guns.modular.ry600.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.stylefeng.guns.modular.ry600.controller.exception.NotAuthedException;
import cn.stylefeng.guns.modular.ry600.service.IOrdercheckService;
import cn.stylefeng.guns.modular.system.dao.ClientcustomerMapper;
import cn.stylefeng.guns.modular.system.dao.ClientproductMapper;
import cn.stylefeng.guns.modular.system.dao.ErpCustomerMapper;
import cn.stylefeng.guns.modular.system.dao.ErpProductMapper;
import cn.stylefeng.guns.modular.system.dao.OrdercheckMapper;
import cn.stylefeng.guns.modular.system.model.Clientproduct;
import cn.stylefeng.guns.modular.system.model.ErpCustomer;
import cn.stylefeng.guns.modular.system.model.ErpProduct;
import cn.stylefeng.guns.modular.system.model.Order;
import cn.stylefeng.guns.modular.system.model.Ordercheck;
import cn.stylefeng.guns.modular.system.model.Orderitem;
import cn.stylefeng.roses.core.mutidatasource.annotion.DataSource;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author stylefeng
 * @since 2019-01-14
 */
@Service
public class OrdercheckServiceImpl extends ServiceImpl<OrdercheckMapper, Ordercheck> implements IOrdercheckService {
	@Autowired
	private OrdercheckMapper ordercheckMapper;
	@Autowired
	private ClientproductMapper clientproductMapper;
	@Autowired
	private ErpCustomerMapper erpCustomerMapper;
	@Autowired
	private ErpProductMapper erpProductMapper;
	
	
	@DataSource(name = "#dataSource")
	public int checkBaseMsg(Set<String> erpProduct,Set<String> clientProduct,Set<String> lsProduct,String cumId,String clientId,String zt,String dataSource){
		Map<String,Object> msg=new HashMap<String,Object>();
		//拉取绑定商品
		if(clientProduct.size()>0){
			msg.put("checkProductId", lsProduct);
			msg.put("clientId", clientId);
			List<Clientproduct> cpl=clientproductMapper.selectByMsg(msg);
			/*for(int i=0;i<cpl.size();i++){
				if(clientProduct.contains(cpl.get(i).getProductId())){
					clientProduct.remove(cpl.get(i).getProductId());
				}
			}*/
			cpl.stream().filter(c -> clientProduct.contains(c.getProductId())).forEach(c -> clientProduct.remove(c.getProductId()));
		}
		
		//拉取erp商品
		if(erpProduct.size()>0){
			msg.clear();
			msg.put("checkProductId", lsProduct);
			List<ErpProduct> epl=erpProductMapper.selectByMsg(msg);
			//拉取erp客户
			/*for(int i=0;i<epl.size();i++){
				if(erpProduct.contains(epl.get(i).getProductId())){
					erpProduct.remove(epl.get(i).getProductId());
				}
			}*/
			epl.stream().map(ErpProduct::getProductId).filter(erpProduct::contains).forEach(erpProduct::remove);
		}
		if(cumId.substring(0, 1).equals(zt)){
			msg.clear();
			msg.put("orgId", cumId);
			List<ErpCustomer> ecl=erpCustomerMapper.selectByMsg(msg);
			if(ecl.size()==0){
				return 2;
//				throw new NotAuthedException("客户不存在erp，拒绝接收");
			}
		}
		//拉取绑定客户
			msg.clear();
			msg.put("org_id", cumId);
			msg.put("client_id", clientId);
			msg.put("zt", zt+"%");
			Set<Map<String,Object>> ms=ordercheckMapper.getOrgByBind(msg);
		if(cumId.substring(0, 1).equals(zt)&&ms.size()==0){
			return 3;
//			throw new NotAuthedException("客户不属于连锁客户，拒绝接收");
		}else if(ms.size()==0){
			return 4;
//			throw new NotAuthedException("客户未绑定账套");
		}
		return 1;
	}
}
